class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length();
        int n = text2.length();
        // 状态表示 - [0, i] 区间 s1 中所有子序列 与[0, j] 区间 s2 中所有子序列中最长公共子序列的长度
        int[][] dp = new int[m + 1][n + 1];
        // 初始化
        String s1 = " " + text1;
        String s2 = " " + text2;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(s1.charAt(i) == s2.charAt(j)){
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }else{
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[m][n];
    }
}